/**
 * @author rxliuli
 */
const app = (() => {
    const {
        DateFormatter: {
            dateTimeFormatter,
            dateFormatter,
        },
        arrayToMap,
        groupBy,
    } = rx
    return new Vue({
        el: '#app',
        data: {
            activeName: "first",
            baseCustomer: new BaseCustomer(),
            baseHousesHoldInfoList: [],
            tabStatus: 1,
        },
        methods: {
            strFormat(str, fmt) {
                return dateFormatter.strFormat(str, fmt)
            },
            updateBaseCustomer() {
                this.updateBaseServiceInfo(this.baseHousesHoldInfoList[0])
            },
            updateBaseServiceInfo(baseHousesHoldInfo) {
                forward('/basic-data/modify/admin_user_info_modify', {
                    ...this.baseCustomer,
                    baseHousesHoldInfo: baseHousesHoldInfo,
                })
            },
            addBaseServiceInfo() {
                forward('/basic-data/add/admin_user_info_add_service', this.baseCustomer)
            },
            async deleteBaseServiceInfo({id}) {
                const result = await rxPrompt.confirm('你确定要删除吗？删除后就不能恢复了', ['确认', '取消'])
                if (!result) {
                    return;
                }
                const res = await baseHousesHoldInfoApi.delete(id)
                if (res.code !== 200 || !res.data) {
                    await rxPrompt.dangerMsg(`删除失败,${res.msg}`)
                    return
                }
                await rxPrompt.successMsg('删除成功')
                this.baseHousesHoldInfoList = this.baseHousesHoldInfoList.filter(item => item.id !== id)
            },
            async initBaseServiceInfo(baseServiceInfoList, baseHousesHoldInfoList, baseFrameInfoMap, baseIpTransInfoMap, baseServiceDomainMap) {
                const provinceCityArea = {
                    provinceMap: arrayToMap(await sysConfProvinceApi.selectList(), ({id}) => id, ({name}) => name),
                    cityMap: arrayToMap(await sysConfCityApi.selectList(), ({id}) => id, ({name}) => name),
                    areaMap: arrayToMap(await sysConfAreaApi.selectList(), ({id}) => id, ({name}) => name),
                }
                const baseServiceInfoMap = arrayToMap(baseServiceInfoList, ({holdId}) => holdId)
                this.baseHousesHoldInfoList = baseHousesHoldInfoList
                    .map(({id, ...others}) => ({
                        ...others,
                        ...baseServiceInfoMap.get(id),
                        id,
                    }))
                    .map(baseHousesHoldInfo => ({
                        ...baseHousesHoldInfo,
                        setModeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.setModeMap)(baseHousesHoldInfo.setMode),
                        regTypeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.regTypeMap)(baseHousesHoldInfo.regType),
                        businessTypeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.businessTypeMap)(baseHousesHoldInfo.businessType),
                        serviceContentSet: baseHousesHoldInfo.serviceContent.split(',').map(Math.floor).map(fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.serviceContentMap)),
                        location: `${provinceCityArea.provinceMap.get(baseHousesHoldInfo.houseProvince) || '未填写'}-${provinceCityArea.cityMap.get(baseHousesHoldInfo.houseCity) || '未填写'}-${provinceCityArea.areaMap.get(baseHousesHoldInfo.houseCounty) || '未填写'}`,
                        distributeTimeStr: dateTimeFormatter.strFormat(baseHousesHoldInfo.distributeTime),
                        baseFrameInfo: baseFrameInfoMap.get(baseHousesHoldInfo.frameInfoId) || {},
                        baseIpTransInfoList: baseIpTransInfoMap.get(baseHousesHoldInfo.id),
                        baseServiceDomainList: baseServiceDomainMap.get(baseServiceInfoMap.get(baseHousesHoldInfo.id).id) || [],
                    }))
            },
            initBaseHousesHoldInfo(baseHousesHoldInfoList, baseFrameInfoMap, baseIpTransInfoMap) {
                this.baseHousesHoldInfoList = baseHousesHoldInfoList
                    .map(baseHousesHoldInfo => ({
                        ...baseHousesHoldInfo,
                        setModeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.setModeMap)(baseHousesHoldInfo.setMode),
                        regTypeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.regTypeMap)(baseHousesHoldInfo.regType),
                        businessTypeStr: fieldFormatter.generator(fieldFormatter.baseHousesHoldInfo.businessTypeMap)(baseHousesHoldInfo.businessType),
                        ...baseHousesHoldInfo,
                        distributeTimeStr: dateTimeFormatter.strFormat(baseHousesHoldInfo.distributeTime),
                        baseFrameInfo: baseFrameInfoMap.get(baseHousesHoldInfo.frameInfoId) || {},
                        baseIpTransInfoList: baseIpTransInfoMap.get(baseHousesHoldInfo.id),
                    }))
            },
            async initialBasicServiceInfo() {
                const {
                    baseHousesHoldInfoList,
                    baseFrameInfoList,
                    baseServiceInfoList,
                    baseIpTransInfoList,
                    baseServiceDomainList,
                    ...other
                } = await baseCustomerApi.getForAllBaseHousesHoldInfo(forward.getData().id)
                this.baseCustomer = {
                    ...other,
                    userTypeStr: fieldFormatter.generator(fieldFormatter.baseCustomer.userTypeMap)(other.userType),
                    unitNatureStr: fieldFormatter.generator(fieldFormatter.baseCustomer.unitNatureMap)(other.unitNature),
                    idTypeStr: fieldFormatter.generator(fieldFormatter.baseCustomer.idTypeMap)(other.idType),
                }
                const baseFrameInfoMap = arrayToMap(baseFrameInfoList, item => item.id)
                const baseIpTransInfoMap = groupBy(baseIpTransInfoList, item => item.holdId)
                if (this.baseCustomer.userType === 1) {
                    const baseServiceDomainMap = groupBy(baseServiceDomainList, item => item.serviceId)
                    this.initBaseServiceInfo(baseServiceInfoList, baseHousesHoldInfoList, baseFrameInfoMap, baseIpTransInfoMap, baseServiceDomainMap)
                } else {
                    this.initBaseHousesHoldInfo(baseHousesHoldInfoList, baseFrameInfoMap, baseIpTransInfoMap)
                }
            },
        },
        computed: {
            fieldFormatter() {
                return fieldFormatter
            },
        },
        mounted() {
            this.initialBasicServiceInfo()
        },
    })
})()